package com.kehao.others.tencent;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

public class Question2 {
    int getNumber(int[] a){
        List<Integer> list = new ArrayList();
        for (int i = 0; i < a.length; i++) {
            list.add(a[i]);
        }
        while (list.size()>1){
            if(list.size()==2){
                return list.get(1);
            }
            for (int i = list.size()-1; i+1 >2 ; i--) {
                double sqrt = Math.sqrt(i + 1);
                for (int j = (int) sqrt; j >1 ; j--) {
                    if((i+1)%j==0){
                        list.remove(i);
                        break;
                    }
                }
            }
            list.remove(0);
        }
       return list.get(0);
    }

    @Test
    public void test1(){
        int[] a = {1,2,3,4};
        System.out.println(getNumber(a));
    }

    @Test
    public void test2(){
        int[] a = {3,1,1,4,5,6};
        System.out.println(getNumber(a));
    }
}
